package psu.kit;

public class ArrayHandler {

    public int handelArray(int[] array) {
        int number = countNonZeroElement(array);
        if (number != array.length && number != 0) {
            shiftZerosElements(array);
        }
        return number;
    }

    private int countNonZeroElement(int array[]) {
        int number = 0;
        for (int ArrayElements : array) {
            if (ArrayElements != 0) {
                number++;
            }
        }
        return number;
    }

    void shiftZerosElements(int[] array) {
        int k = array.length - 1;
        for (int i = 0; i < k; ++i) {
            if (array[i] == 0) {
                while (array[k] == 0) {
                    k--;
                }
                array[i] = array[k];
                array[k] = 0;
                k--;
            }
        }
    }
}
